package com.liunian.algorithmstudy.stackandqueue;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {

    private static Queue<Integer> queueIn;
    private static Queue<Integer> queueOut;

    public MyStack() {
        queueIn = new LinkedList<>();
        queueOut = new LinkedList<>();
    }
    
    public void push(int x) {
        queueIn.add(x);
    }
    
    public int pop() {
        int size = queueIn.size();
        if (size > 1) {
            for (int i = 0; i < size; i++) {
                queueOut.add(queueIn.remove());
            }
            for (int i = 0; i < size - 1; i++) {
                queueIn.add(queueOut.remove());
            }
            return queueOut.remove();
        }
        return queueIn.remove();
    }
    
    public int top() {
        int pop = pop();
        queueIn.add(pop);
        return pop;
    }
    
    public boolean empty() {
        if (queueIn.isEmpty() && queueOut.isEmpty()) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */